package com.lu.statistics.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lu.statistics.entity.MtDishDescription;
import com.lu.statistics.entity.MtDishInfo;
import com.lu.statistics.entity.enums.ChoiceTypeEnum;
import com.lu.statistics.entity.enums.DishTypeEnum;
import com.lu.statistics.entity.enums.ProductTypeEnum;
import com.lu.statistics.entity.front.response.DishDescriptionResponse;
import com.lu.statistics.entity.front.response.DishIGroupResponse;
import com.lu.statistics.entity.front.response.DishInfoResponse;
import com.lu.statistics.entity.front.response.ProGroupResponse;
import com.lu.statistics.mapper.MtDishCategoriesMapper;
import com.lu.statistics.mapper.MtDishDescriptionMapper;
import com.lu.statistics.mapper.MtDishInfoMapper;
import com.lu.statistics.service.DishInfoService;
import com.lu.statistics.service.GroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;


@Service
public class DishInfoServiceImpl extends ServiceImpl<MtDishInfoMapper, MtDishInfo> implements DishInfoService, GroupService {
    @Autowired
    private MtDishInfoMapper dishInfoMapper;
    @Autowired
    private MtDishCategoriesMapper dishCategoriesMapper;
    @Autowired
    private MtDishDescriptionMapper descriptionMapper;

    @Override
    public DishIGroupResponse queryGroupDish(MtDishInfo dishInfo, int pageNo, int pageSize) {
        Page<MtDishInfo> page = new Page(pageNo, pageSize);
        DishIGroupResponse response = new DishIGroupResponse();
        List<DishInfoResponse> cuisineList = new ArrayList<>();
        List<DishInfoResponse> liquorList = new ArrayList<>();
        IPage pageDish = dishInfoMapper.queryDishInfoList(page, dishInfo.getCategoriesId(), dishInfo.getDishName(), dishInfo.getChoiceType());
        if (pageDish.getRecords() != null && pageDish.getRecords().size() > 0) {
            for (Object item : pageDish.getRecords()) {
                MtDishInfo dish = (MtDishInfo) item;
                DishInfoResponse dishInfoResponse = new DishInfoResponse();
                String url = "/api/file/previewPhoto?fileFullPath=" + dish.getImgUrl();
                //菜系
                if (dish.getDishType().equals(DishTypeEnum.CUISINE.name())) {
                    BeanUtils.copyProperties(dish, dishInfoResponse);
                    dishInfoResponse.setImgUrl(url);
                    cuisineList.add(dishInfoResponse);
                } else {//酒水
                    BeanUtils.copyProperties(dish, dishInfoResponse);
                    dishInfoResponse.setImgUrl(url);
                    liquorList.add(dishInfoResponse);
                }

            }
            response.setCuisineList(cuisineList);
            response.setLiquorList(liquorList);
            response.setDishTotal(pageDish.getTotal());
        }
        return response;
    }

    @Override
    public MtDishInfo queryDishInfoById(int id) {
        return dishInfoMapper.selectById(id);
    }

    @Override
    public IPage<MtDishInfo> queryDishInfoList(MtDishInfo dishInfo, int pageNo, int pageSize) {
        Page<MtDishInfo> page = new Page(pageNo, pageSize);
        IPage dishPage = dishInfoMapper.queryDishInfoList(page, dishInfo.getCategoriesId(), dishInfo.getDishName(), dishInfo.getChoiceType());
        return dishPage;
    }

    @Override
    public DishInfoResponse queryDishById(int id) {
        DishInfoResponse dishInfoResponse = new DishInfoResponse();
        MtDishInfo dishInfo = dishInfoMapper.selectById(id);
        if (dishInfo != null) {
            BeanUtils.copyProperties(dishInfo, dishInfoResponse);
            String url = "/api/file/previewPhoto?fileFullPath=" + dishInfo.getImgUrl();
            dishInfoResponse.setImgUrl(url);
            List<MtDishDescription> mtDishDescriptions = descriptionMapper.queryDishDescriptions(id, ChoiceTypeEnum.SINGLE_DISH.name());
            if (mtDishDescriptions != null && mtDishDescriptions.size() > 0) {
                List<DishDescriptionResponse> newDescrList = new ArrayList<>();
                for (MtDishDescription description : mtDishDescriptions) {
                    DishDescriptionResponse response = new DishDescriptionResponse();
                    BeanUtils.copyProperties(description, response);
                    newDescrList.add(response);
                }
                dishInfoResponse.setDescList(newDescrList);
            }
        }
        return dishInfoResponse;
    }

    @Override
    public ProGroupResponse queryGroupList(int pageNo, int pageSize) {
        ProGroupResponse groupResponse = new ProGroupResponse();
        MtDishInfo dishInfo = new MtDishInfo();
        dishInfo.setCategoriesId(0);
        dishInfo.setChoiceType(ChoiceTypeEnum.SINGLE_DISH.name());
        DishIGroupResponse dishIGroupResponse = queryGroupDish(dishInfo, pageNo, pageSize);
        groupResponse.setCuisineList(dishIGroupResponse.getCuisineList());
        groupResponse.setDishTotal(dishIGroupResponse.getDishTotal());
        return groupResponse;
    }

    @Override
    public ProductTypeEnum getType() {
        return ProductTypeEnum.DIYMEAL;
    }
}
